<?php



namespace app\api\controller;

use app\model\hotel\Hotel as HotelModel;
use app\model\shop\ShopApply as ShopApplyModel;
use app\model\shop\ShopCategory as ShopCategoryModel;
use app\model\shop\ShopGroup as ShopGroupModel;


class Apply extends BaseApi
{
    public function __construct()
    {
        //执行父类构造函数
        parent::__construct();
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) {
            echo $this->response($token);
            exit;
        }
    }

    public function config()
    {
        //店铺主营行业
        $shop_category_model = new ShopCategoryModel();
        $shop_category = $shop_category_model->getCategoryList('', 'category_id,category_name', 'sort asc', '');
        $data[ 'shop_category' ] = $shop_category[ 'data' ];

        //店铺等级
        $shop_group_model = new ShopGroupModel();
        $shop_group = $shop_group_model->getGroupList([ ], 'group_id,group_name');
        $shop_group = $shop_group[ 'data' ];
        $data[ 'shop_group' ] = $shop_group;

        //证件类型
        $hotel_model = new HotelModel();
        $id_card_type_list = $hotel_model->idCardType();
        $data[ 'id_card_type_list' ] = $id_card_type_list;
        return $this->response($this->success($data));
    }

    //申请信息
    public function applyinfo()
    {
        $shop_type = isset($this->params[ 'shop_type' ]) ? $this->params[ 'shop_type' ] : 0;
        $model = new ShopApplyModel();
        $result = $model->getApplyInfo([
            ['member_id','=',$this->member_id],
            ['shop_type','=',$shop_type]
        ]);
        if ($result['data']){
            $val                                      = transformSeckillTime($result['data']);
            $result['data']['start_time_show'] = "{$val['start_hour']}:{$val['start_minute']}";
            $result['data']['end_time_show']   = "{$val['end_hour']}:{$val['end_minute']}";
        }

        return $this->response($result);

    }

    //申请
    public function apply()
    {
        $apply_data = [
            'member_id'=>$this->member_id,
            'shop_type' => isset($this->params[ 'shop_type' ]) ? $this->params[ 'shop_type' ] : 0,
            'shop_name' => isset($this->params[ 'shop_name' ]) ? $this->params[ 'shop_name' ] : '',//申请店铺名称
            'category_name' => isset($this->params[ 'category_name' ]) ? $this->params[ 'category_name' ] : '',//店铺分类名称
            'category_id' => isset($this->params[ 'category_id' ]) ? $this->params[ 'category_id' ] : '',//店铺分类id
            'group_name' => isset($this->params[ 'group_name' ]) ? $this->params[ 'group_name' ] : '',//开店套餐名称
            'group_id' => isset($this->params[ 'group_id' ]) ? $this->params[ 'group_id' ] : '',// 开店套餐ID
            'apply_state' => 1,//审核状态（待审核）
            "province" => isset($this->params[ 'province' ]) ? $this->params[ 'province' ] : '',
            "province_name" => isset($this->params[ 'province_name' ]) ? $this->params[ 'province_name' ] : '',
            "city" => isset($this->params[ 'city' ]) ? $this->params[ 'city' ] : '',
            "city_name" => isset($this->params[ 'city_name' ]) ? $this->params[ 'city_name' ] : '',
            "district" => isset($this->params[ 'district' ]) ? $this->params[ 'district' ] : '',
            "district_name" => isset($this->params[ 'district_name' ]) ? $this->params[ 'district_name' ] : '',
            "address" => isset($this->params[ 'address' ]) ? $this->params[ 'address' ] : '',
            "full_address" => isset($this->params[ 'full_address' ]) ? $this->params[ 'full_address' ] : '',
            "longitude" => isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : '',
            "latitude" => isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : '',
            "start_time" => isset($this->params[ 'start_time' ]) ? $this->params[ 'start_time' ] : '',
            "end_time" => isset($this->params[ 'end_time' ]) ? $this->params[ 'end_time' ] : '',
            "name" => isset($this->params[ 'name' ]) ? $this->params[ 'name' ] : '',
            "mobile" => isset($this->params[ 'mobile' ]) ? $this->params[ 'mobile' ] : '',
            "avatar" => isset($this->params[ 'avatar' ]) ? $this->params[ 'avatar' ] : '',
            "banner" => isset($this->params[ 'banner' ]) ? $this->params[ 'banner' ] : '',
            'contacts_card_electronic_2' => isset($this->params[ 'contacts_card_electronic_2' ]) ? $this->params[ 'contacts_card_electronic_2' ] : '',//申请人身份证正面
            'contacts_card_electronic_3' => isset($this->params[ 'contacts_card_electronic_3' ]) ? $this->params[ 'contacts_card_electronic_3' ] : '',//申请人身份证反面
            'contacts_card_no' => isset($this->params[ 'contacts_card_no' ]) ? $this->params[ 'contacts_card_no' ] : '',
            'contacts_card_name' => isset($this->params[ 'contacts_card_name' ]) ? $this->params[ 'contacts_card_name' ] : '',
            'contacts_card_type' => isset($this->params[ 'contacts_card_type' ]) ? $this->params[ 'contacts_card_type' ] : '',
            'contacts_card_start_time' => isset($this->params[ 'contacts_card_start_time' ]) ? $this->params[ 'contacts_card_start_time' ] : '',
            'contacts_card_end_time' => isset($this->params[ 'contacts_card_end_time' ]) ? $this->params[ 'contacts_card_end_time' ] : '',

            'settlement_bank_account_name' => isset($this->params[ 'settlement_bank_account_name' ]) ? $this->params[ 'settlement_bank_account_name' ] : '',//开户人
            'settlement_bank_account_number' => isset($this->params[ 'settlement_bank_account_number' ]) ? $this->params[ 'settlement_bank_account_number' ] : '',//银行卡号
            'settlement_bank_name' => isset($this->params[ 'settlement_bank_name' ]) ? $this->params[ 'settlement_bank_name' ] : '',//开户行
            'settlement_bank_type' => isset($this->params[ 'settlement_bank_type' ]) ? $this->params[ 'settlement_bank_type' ] : '',//开户行
            'settlement_bank_img' => isset($this->params[ 'settlement_bank_img' ]) ? $this->params[ 'settlement_bank_img' ] : '',//开户行

            'cert_type' => isset($this->params[ 'cert_type' ]) ? $this->params[ 'cert_type' ] : 1,//申请类型1.个人店铺 2.企业店铺
            'business_licence_number' => isset($this->params[ 'business_licence_number' ]) ? $this->params[ 'business_licence_number' ] : '',//统一社会信用码 input
            'business_licence_number_electronic' => isset($this->params[ 'business_licence_number_electronic' ]) ? $this->params[ 'business_licence_number_electronic' ] : '',//营业执照电子版

            'license_time_type' => isset($this->params[ 'license_time_type' ]) ? $this->params[ 'license_time_type' ] : 1,
            'license_start_date' => isset($this->params[ 'license_start_date' ]) ? $this->params[ 'license_start_date' ] : '',
            'license_end_date' => isset($this->params[ 'license_end_date' ]) ? $this->params[ 'license_end_date' ] : '',

            'contact_email' => isset($this->params[ 'contact_email' ]) ? $this->params[ 'contact_email' ] : '',
            'legal_addr' => isset($this->params[ 'legal_addr' ]) ? $this->params[ 'legal_addr' ] : '',
            'legal_cert_validity_type' => isset($this->params[ 'legal_cert_validity_type' ]) ? $this->params[ 'legal_cert_validity_type' ] : 1,
            'busi_type' => isset($this->params[ 'busi_type' ]) ? $this->params[ 'busi_type' ] : 0,
            'bank_province' => isset($this->params[ 'bank_province' ]) ? $this->params[ 'bank_province' ] : '',
            'bank_city' => isset($this->params[ 'bank_city' ]) ? $this->params[ 'bank_city' ] : '',
            'bank_district' => isset($this->params[ 'bank_district' ]) ? $this->params[ 'bank_district' ] : '',
            'bank_province_name' => isset($this->params[ 'bank_province_name' ]) ? $this->params[ 'bank_province_name' ] : '',
            'bank_city_name' => isset($this->params[ 'bank_city_name' ]) ? $this->params[ 'bank_city_name' ] : '',
            'bank_district_name' => isset($this->params[ 'bank_district_name' ]) ? $this->params[ 'bank_district_name' ] : '',
        ];

        $start_time = explode(':',$apply_data['start_time']);

        $apply_data['start_time'] = $start_time[0] * 3600 + $start_time[1] * 60;

        $end_time = explode(':',$apply_data['end_time']);

        $apply_data['end_time'] = $end_time[0] * 3600 + $end_time[1] * 60;

        //dump($apply_data);die;
        if (empty($apply_data['mobile'])) {
            return $this->response($this->error('', '请输入联系人手机号'));
        }
        if (!validatePhoneNumber($apply_data['mobile'])) {
            return $this->response($this->error('', '手机号有误'));
        }
        if (empty($apply_data['contacts_card_no'])) {
            return $this->response($this->error('', '请输入证件号'));
        }
        if (!validateIDCard($apply_data['contacts_card_no'])) {
            return $this->response($this->error('', '证件号无效'));
        }

        $model = new ShopApplyModel();
        $result = $model->newapply($apply_data);
        return $this->response($result);
    }


}